<template>
	<view>
		<view class="fllow_form">
			<view class="reportTitle">
				<view class="title" style="background-color: #AF99FE;" v-if="typename == '日报'">
					<text>{{typename}}</text>
				</view>
				<view class="title" style="background-color: #FBB545;" v-if="typename == '周报'">
					<text>{{typename}}</text>
				</view>
				<view class="title" style="background-color: #4AD49D;" v-if="typename == '月报'">
					<text>{{typename}}</text>
				</view>
				<!-- <view class="title" style="background-color: #FF6E7F;" v-if="typename == '季报'">
					<text>{{typename}}</text>
				</view>
				<view class="title" style="background-color: #54B1F7;" v-if="typename == '年报'">
					<text>{{typename}}</text>
				</view> -->
			</view>
		</view>
		
		<form-item v-for="(item,index) in listForm" :height="200" :key="index" :objInfo="item" @changeData="changeData"></form-item>
		
		<view class="affiliation">
			<view class="aff_head">提醒谁看<text style="color: #f00;">*</text></view>
			<view class="aff_con">
				<view class="aff_item affbox" v-for="(item,index) in remindPeople" :key="index">
					<image :src="BASE_IMG_URL+'close.png'" class="delremind" v-if="item.isDel" @click="delRemind(index)" mode=""></image>
					<image :src="item.img ? item.img : BASE_IMG_URL+'headImg.png'"></image>
					<view>{{item.name}}</view>
				</view>
				<view class="aff_item aff_last" @click="moreClick">
					<image :src="BASE_IMG_URL+'morebtn.png'"></image>
					<view>更多</view>
				</view>
			</view>
		</view>
		<view class="btn_g bg-blue button-hover round" @click="$noMultipleClicks(subSave)">
			保存
		</view>
		
	</view>
</template>

<script>
	import { netSetForm, netCreateWork, netStaffDefaultReviewer,netCreateStraf,netWorkDraftDetail } from '@/api/index.js'
	import formItem from '@/components/form/formitem.vue'
	import { BASE_IMG_URL } from '@/util/api.js'
	
	import {
		checkPhone,
		checkNumber,
		checkCNPhone
	} from '@/util/util.js'
	let T;
	export default {
		components:{
			formItem
		},
		data() {
			return{
				BASE_IMG_URL:BASE_IMG_URL,
				noClick:true,    //防止  重复点击
				info:{},
				listForm:[],
				remindPeople: [], //提醒谁看
				typeList:[
					{id:1,name:'日报',value:'daily'},
					{id:2,name:'周报',value:'weekly'},
					{id:3,name:'月报',value:'monthly'},
					{id:4,name:'季报',value:'quarterly'},
					{id:5,name:'年报',value:'yearly'}
				],
				typeIndex:0,
				typename:'日报',
			}
		},
		onLoad(param) {
			if(param.type ==1){
				this.typename = '日报';
				this.typeIndex= 0;
			}else if(param.type ==2){
				this.typename = '周报';
				this.typeIndex= 1;
			}else if(param.type ==3){
				this.typename = '月报';
				this.typeIndex= 2;
			}else if(param.type ==4){
				this.typename = '季报';
				this.typeIndex= 3;
			}else if(param.type ==5){
				this.typename = '年报';
				this.typeIndex= 4;
			}
			//表单
			this.getForm()
		
			
			T= setInterval(()=>{
				// this.subSaveDraft()
			},30000)
		},
		onShow() {
			this.remindPeople = []
			//获取  默认需要提醒的人
			this.getDefaultRemind()
			// this.getDetail();
		},
		onUnload() {
			clearInterval(T)
			uni.removeStorageSync('remindlist')
		},
		methods:{
			
			changeType(e) {
				this.typeIndex = e.detail.value
				this.getForm()
			},
			// 选择成员
			moreClick() {
				this.subSaveDraft()
				uni.navigateTo({
					url: '/pagesA/crm/selectMember/selectMember'
				})
			},
			//获取  需要默认提醒的人
			getDefaultRemind() {
				netStaffDefaultReviewer({type:'worklog'}).then(res=>{
					res = res.data
					let arr = uni.getStorageSync('remindlist')?uni.getStorageSync('remindlist'):[]
					arr.forEach((item,index)=>{
						res.forEach(ele=>{
							if(item.id == ele.id){
								item.isDel = false
							}
						})
					})
					this.remindPeople = this.checkReport([...res,...uni.getStorageSync('remindlist')])
				})
			},
			//删除 提醒人
			delRemind(index) {
				this.remindPeople.splice(index, 1)
				uni.setStorageSync('remindlist', this.remindPeople)
			},
			getForm() {
				let params = {
					type: this.typeList[this.typeIndex].value
				}
				netSetForm(params).then(res=>{
					this.listForm = res.data
				})
			},
			changeData(name,value,obj) {
				let arr = this.listForm
				arr.forEach((ele,index) => {
					if(ele.id == obj.id){
						ele.value = value
						this.$set(this.listForm,index,ele)
					}
				})
			},
			subSave() {
				let arr = []
				let params = {}
				this.listForm.forEach(ele=>{
					arr.push({
						name:ele.id,
						required: ele.config.required,
						msg: ele.config.placeholder,
						value:ele.value
					})
				})
				for(let i = 0; i < arr.length ; i++) {
					if(arr[i].required && !arr[i].value){
						uni.showToast({
							title: arr[i].msg,
							icon:'none'
						})
						return
					}
					console.log("------------213")
					if(arr[i].name == 'sale_finish'  && !checkNumber(arr[i].value)) {
						uni.showToast({
							title: '请输入数字',
							icon:'none'
						})
						return
					}
					if(arr[i].name == 'consume_finish'  && !checkNumber(arr[i].value)) {
						uni.showToast({
							title: '请输入数字',
							icon:'none'
						})
						return
					}
					if(arr[i].name == 'member_cnt_finish'  && !checkNumber(arr[i].value)) {
						uni.showToast({
							title: '请输入数字',
							icon:'none'
						})
						return
					}
					
					params[arr[i].name] = arr[i].value
				}
				if(!this.isAftersales){
					let rearr = this.remindPeople
					let remindsid = '' //提醒谁看
					let remarr = []
					rearr.forEach(ele => {
						remarr.push(ele.id)
					})
					if(remarr.length <= 0) {
						uni.showToast({
							title: '请选择提醒谁看',
							icon:'none'
						})
						// return
					} else {
						remindsid = remarr.join(',')
						params.reminds_id = remindsid
						
					}
				}
				params.type = this.typename
				params.log_type = this.typeList[this.typeIndex].id
				netCreateWork(params).then(res=>{
					uni.showToast({
						title: res.message,
						icon:'none'
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},2000)
				})
			},
			//保存草稿
			subSaveDraft() {
				let arr = []
				let params = {}
				this.listForm.forEach(ele=>{
					arr.push({
						name:ele.id,
						required: ele.config.required,
						msg: ele.config.placeholder,
						value:ele.value
					})
				})
				for(let i = 0; i < arr.length ; i++) {
					
					params[arr[i].name] = arr[i].value
				}
				if(!this.isAftersales){
					let rearr = this.remindPeople
					let remindsid = '' //提醒谁看
					let remarr = []
					rearr.forEach(ele => {
						remarr.push(ele.id)
					})
					remindsid = remarr.join(',')
					params.reminds_id = remindsid
				}
				params.type = this.typename
				netCreateStraf(params).then(res=>{})
			},
			//草稿详情
			getDetail() {
				netWorkDraftDetail({type:this.typename}).then(res=>{
					if(res.data){
						this.info = res.data
						if(res.data.type_index){
							this.typeIndex =res.data.type_index
						}
						// if(res.data.staff_info){
						// 	this.remindPeople = [];
						// 	uni.removeStorageSync('remindlist')
						// 	let staff_info = res.data.staff_info
						// 	staff_info.forEach((ele,index) => {
						// 		if(index >0){
						// 			ele.isDel = true
						// 		}else{
						// 			ele.isDel = false
						// 		}
								
						// 	})
						// 	this.remindPeople =staff_info;
						// 	uni.setStorageSync('remindlist', this.remindPeople)
						// }
						this.getForminfo()
						
					}
					
					
				})
			},
			getForminfo() {
				let params = {
					type: this.typeList[this.typeIndex].value
				}
				netSetForm(params).then(res=>{
					res = res.data.data
					if(res){
						this.listForm =[]
					}
					res.forEach(ele=>{
						if(ele.id != 'type'){
							ele.value = this.info[ele.id]
							this.listForm.push(ele)
						}
					})
				})
			},
			
			
		}
	}
</script>


<style lang="scss" scoped>
	.valueActive{
		color:#333;
	}
	.fllow_form {
		display: flex;
		justify-content: space-between;
		background-color: #fff;
		border-bottom: 1rpx solid #EAEAEA;
		padding:30rpx 24rpx;
		:first-child{
			flex-shrink: 0;
		}
		.reportTitle{
			
			.title{
				width: 690rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				border-radius: 16rpx;
				text{
					font-size: 28rpx;
					font-weight: bold;
					color: #fff;
				}
			}
		}
		.form_right {
			display: flex;
			flex-wrap: wrap;
			color: #999;
			text-align: right;
			image {
				width: 40rpx;
				height: 40rpx;
				margin: 28rpx 5rpx 0 0;
			}
		}
		.theme {
			color: #5ca9fe;
		}
		text {
			color: #f00;
			margin-left: 10rpx;
		}
		.form_input {
			input {
				text-align: right;
				margin-right: 10rpx;
			}
		}
	}
	.affiliation {
		background-color: #fff;
		margin-bottom: 20rpx;
		padding-bottom: 40rpx;
	
		.aff_head {
			height: 100rpx;
			line-height: 100rpx;
			padding-left: 30rpx;
		}
	
		.aff_con {
			display: flex;
			text-align: center;
			flex-wrap: wrap;
	
			.aff_item {
				width: 100rpx;
				margin-right: 15rpx;
				margin-bottom:15rpx;
				image {
					margin-bottom: 15rpx;
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
				}
			}
	
			.affbox {
				position: relative;
	
				.delremind {
					width: 32rpx;
					height: 32rpx;
					border-radius: 50%;
					position: absolute;
					right: -16rpx;
					top: -16rpx;
					background: rgba(0, 0, 0, 0.5);
					z-index: 2;
				}
			}
	
			.aff_last {
				image {
					margin-bottom: 15rpx;
					width: 50rpx;
					height: 50rpx;
				}
			}
		}
	}
</style>
